import React, { useRef } from 'react'
// 导入rc-slider-captcha
import SliderCaptcha from 'rc-slider-captcha'
// 导入
import { sleep } from 'ut2'
// 导入create-puzzle
import createPuzzle from 'create-puzzle'
import { RubyOutlined, MehOutlined, SmileOutlined, RedoOutlined, LoadingOutlined } from '@ant-design/icons'
import pic from '../static/image/1.png'
export default function Tuxing(props) {   
    let {getcode,username,password,setVisible,setcode} = props
    console.log("注册",username,password);
    
   

    const offsetXRef = useRef(0); // x 轴偏移值
    // 查看是否在安全距离
    const verifyCaptcha = async (data) => {
        await sleep();
        if (data.x >= offsetXRef.current - 5 && data.x < offsetXRef.current + 5) {
            console.log('验证成功');
            getcode()
            
            setcode(true)
            return Promise.resolve();
        }
        return Promise.reject();
    };
    return (
        <div className='container'>
            <SliderCaptcha
                request={() =>
                    createPuzzle(pic, {
                        format: 'blob'
                    }).then((res) => {
                        offsetXRef.current = res.x
                        return {
                        // 背景图片
                            bgUrl: res.bgUrl,
                            // 核验区域
                            puzzleUrl: res.puzzleUrl
                        };
                    })
                }
                onVerify={(data) => {
                    return verifyCaptcha(data);
                }}
                // ！！！！这里是重点！！！！！
                // bgSize必须和原图片的尺寸一样！！！！！！！！！！！！！！！！！！
                bgSize={{ width: 360, height: 140 }}
                tipIcon={{
                    default: <RubyOutlined />,
                    loading: <LoadingOutlined />,
                    success: <SmileOutlined />,
                    error: <MehOutlined />,
                    refresh: <RedoOutlined />
                }}
                tipText={{
                    default: '向右👉拖动完成拼图',
                    loading: '👩🏻‍💻🧑‍💻努力中...',
                    moving: '向右拖动至拼图位置',
                    verifying: '验证中...',
                    error: '验证失败'
                }}
            // loadingBoxProps={{
            //   icon: <Heart />,
            //   text: "I'm loading"
            // }}
            />
        </div>
    )
}
